package com.shop2cn.app.liveseller.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.app.liveseller.parameter.AddLiveToShopBean;
import com.shop2cn.app.liveseller.parameter.CreateSellerLiveBean;
import com.shop2cn.app.liveseller.service.AddLiveToShopCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.LiveRoomShop2cnCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.livesellerShop2cn.LiveRoom;
import com.ymttest.database.sqlwapper.livesellerShop2cn.LiveRoomShop2cnWapper;
/**
*新增继承直播间
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AddLiveToShop{
	private static AddLiveToShopBean addlivetoshopBean;
	private static AddLiveToShopCall addlivetoshopCall;
	private static LiveRoomShop2cnWapper LiveRoomShop2cnWapper;
	private static int sellerId ;
	private static String token ;
	private static int csId;
	private static String cstoken;
	private static int mchId_Agent;
	@BeforeClass
	public static void setUp()  throws Exception  {
		Logger.createResultFile("新增继承直播间");
		sellerId = GodmchManageCallService.CreateSellerMch();
		token=AuthCallServiceV2.createToken(sellerId, "app");
		csId = GodmchManageCallService.bindClientService(
				sellerId, 1);
		cstoken = AuthCallServiceV2.createToken(csId, "app");
		 mchId_Agent=GodmchManageCallService.createAgent(sellerId);
	}

	@Before
	public void caseUp() {
		addlivetoshopBean=new AddLiveToShopBean();
		addlivetoshopCall=new AddLiveToShopCall();
		LiveRoomShop2cnWapper=new LiveRoomShop2cnWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addlivetoshopCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddLiveToShop_001() {
		Logger.start(true,"新增继承直播间成功liveInherit1");
		try {
			 CreateSellerLiveBean c = LiveRoomShop2cnCallService.initCreatesellerliveBean();
			 c.getLiveDto().setLiveInherit(true);
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(c,sellerId);
			addlivetoshopBean.setLiveRoomId(createlive.getId());
			addlivetoshopBean.setInheritUserId(mchId_Agent);
			addlivetoshopBean.setParentShopId(sellerId);
			addlivetoshopCall.setData(addlivetoshopBean);
			String head="accesstoken="+token;
			addlivetoshopCall.addHeader("ymt-pars", head);
			addlivetoshopCall.callService();			
			Logger.verifyEquals("{}", addlivetoshopCall.getReturnData(), "验证Code");
			List<Map> map1 = LiveRoomShop2cnWapper.selectLiveInheritrelation(createlive.getId(), mchId_Agent);
			Logger.debug(map1);
			Logger.verifyEquals(map1.get(0).get("Status").toString(), "1", "验证关系");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AddLiveToShop_002() {
		Logger.start(true,"不输入liveRoomId失败");
		try {
			addlivetoshopBean.setInheritUserId(mchId_Agent);
			addlivetoshopBean.setParentShopId(sellerId);
			addlivetoshopCall.setData(addlivetoshopBean);
			String head="accesstoken="+token;
			addlivetoshopCall.addHeader("ymt-pars", head);
			addlivetoshopCall.callService();			
			Logger.verifyEquals("400", addlivetoshopCall.getString("code"), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AddLiveToShop_003() {
		Logger.start(true,"不输入inheritUserId失败");
		try {
			addlivetoshopBean.setLiveRoomId(1);
//			addlivetoshopBean.setInheritUserId(mchId_Agent);
			addlivetoshopBean.setParentShopId(sellerId);
			addlivetoshopCall.setData(addlivetoshopBean);
			String head="accesstoken="+token;
			addlivetoshopCall.addHeader("ymt-pars", head);
			addlivetoshopCall.callService();			
			Logger.verifyEquals("400", addlivetoshopCall.getString("code"), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AddLiveToShop_004() {
		Logger.start(true,"新增继承直播间失败liveInherit0");
		try {
			 CreateSellerLiveBean c = LiveRoomShop2cnCallService.initCreatesellerliveBean();
			 c.getLiveDto().setLiveInherit(false);
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(c,sellerId);
			addlivetoshopBean.setLiveRoomId(createlive.getId());
			addlivetoshopBean.setInheritUserId(mchId_Agent);
			addlivetoshopBean.setParentShopId(sellerId);
			addlivetoshopCall.setData(addlivetoshopBean);
			String head="accesstoken="+token;
			addlivetoshopCall.addHeader("ymt-pars", head);
			addlivetoshopCall.callService();			
			Logger.verifyEquals("{}", addlivetoshopCall.getReturnData(), "验证Code");
			List<Map> map1 = LiveRoomShop2cnWapper.selectLiveInheritrelation(createlive.getId(), mchId_Agent);
			Logger.debug(map1);
			//TODO 这里应该失败
			Logger.verifyEquals(map1.get(0).get("Status"), "0", "验证关系");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AddLiveToShop_005() {
		Logger.start(true,"不输入parentShopId");
		try {
			addlivetoshopBean.setLiveRoomId(1);
			addlivetoshopBean.setInheritUserId(mchId_Agent);
//			addlivetoshopBean.setParentShopId(sellerId);
			addlivetoshopCall.setData(addlivetoshopBean);
			String head="accesstoken="+token;
			addlivetoshopCall.addHeader("ymt-pars", head);
			addlivetoshopCall.callService();			
			Logger.verifyEquals("{}", addlivetoshopCall.getReturnData(), "验证Code");	
			List<Map> map1 = LiveRoomShop2cnWapper.selectLiveInheritrelation(1, mchId_Agent);
			Logger.debug(map1);
			//兼容以前的协议 这里是可以成功的
			Logger.verifyEquals(map1.get(0).get("Status").toString(), "1", "验证关系");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddLiveToShop_006() {
		Logger.start(true,"新增继承直播间成功liveInherit1-自动铺货");
		try {
			 CreateSellerLiveBean c = LiveRoomShop2cnCallService.initCreatesellerliveBean();
			 c.getLiveDto().setLiveInherit(true);
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(c,sellerId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId, "autotest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(), sellerId, Arrays.asList(pid));
			
			addlivetoshopBean.setLiveRoomId(createlive.getId());
			addlivetoshopBean.setInheritUserId(mchId_Agent);
			addlivetoshopBean.setParentShopId(sellerId);
			addlivetoshopCall.setData(addlivetoshopBean);
			String head="accesstoken="+token;
			addlivetoshopCall.addHeader("ymt-pars", head);
			addlivetoshopCall.callService();			
			Logger.verifyEquals("{}", addlivetoshopCall.getReturnData(), "验证Code");
			List<Map> map1 = LiveRoomShop2cnWapper.selectLiveInheritrelation(createlive.getId(), mchId_Agent);
			Logger.debug(map1);
			Logger.verifyEquals(map1.get(0).get("Status").toString(), "1", "验证关系");
			
			// 验证发送铺货消息
			MessageListBean MessageListBean = MqQueryCallService.initImportAgentproduct(pid, mchId_Agent);
			List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if (joa.size() == 0) {
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele -> {
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(joa.size()>=1, true, "回调了自动导入" + MessageListBean.toString());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}